package com.realdolmen.whiteyellowcross.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.realdolmen.whiteyellowcross.appengine.datastore.PersonelAppEngineDataStore;
import com.realdolmen.whiteyellowcross.appengine.datastore.WaitWorkShiftDayPlanAppEngineDataStore;
import com.realdolmen.whiteyellowcross.datastore.PersonelDataStore;
import com.realdolmen.whiteyellowcross.datastore.WaitWorkShiftDayPlanDataStore;
import com.realdolmen.whiteyellowcross.model.Personel;
import com.realdolmen.whiteyellowcross.model.WaitMonthPlanning;
import com.realdolmen.whiteyellowcross.model.WaitWorkShiftDayPlan;
import com.realdolmen.whiteyellowcross.service.PersonelPersistenceBasedService;
import com.realdolmen.whiteyellowcross.service.PersonelService;
import com.realdolmen.whiteyellowcross.service.PlanningService;
import com.realdolmen.whiteyellowcross.service.PlanningPersistenceBasedService;

@SuppressWarnings("serial")
public class PersonelServlet extends HttpServlet {

	
	private static final String LOAD_ALL_PERSONEL = "loadPersonel";
	private static final String UPDATE_PERSONEL = "updatePersonel";
	private static final String ADD_PERSONEL = "addPersonel";
	

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		StringBuffer resultingHtml = new StringBuffer();
		
		String action = req.getParameter("action");

		if (LOAD_ALL_PERSONEL.equals(action)) {
			performLoadWaitCalendar(resultingHtml, req);
		}else if (UPDATE_PERSONEL.equals(action)) {
			performWaitCalendarTotals(resultingHtml, req);
		}
		try {
			if (resultingHtml != null && resultingHtml.toString().length() > 0) {
				resp.getWriter().print(resultingHtml.toString());
				resp.getWriter().flush();
			}
		} catch (IOException ioe) {

		}
		
	}
	
	private void generateWaitCalendarHeader(StringBuffer resultingHtml){
		resultingHtml.append("<table id=\"calendar_table_header\">\n");
		resultingHtml.append("\t<thead>\n");
		resultingHtml.append("\t\t<tr>\n");
		resultingHtml.append("\t\t\t<th class=\"table_header_links\" onclick=\"previousYear();\">Vorige maand</th>\n");
		resultingHtml.append("\t\t\t<th class=\"table_header_links\" onclick=\"previousMonth();\">Vorig jaar</th>\n");
		resultingHtml.append("\t\t\t<th>Wachtkalendar</th>\n");
		resultingHtml.append("\t\t\t<th class=\"table_header_links\" onclick=\"nextMonth();\">Volgende maand</th>\n");
		resultingHtml.append("\t\t\t<th class=\"table_header_links\" onclick=\"nextYear();\">Volgend jaar</th>\n");
		resultingHtml.append("\t\t</tr>\n");
		resultingHtml.append("\t</thead>\n");	
		resultingHtml.append("</table>\n");
	}
	
	private void generateWorkShiftPlan(Personel[] personelList,WaitWorkShiftDayPlan morningWorkShiftPlan,WaitMonthPlanning waitMonthPlanning,StringBuffer resultingHtml){
		
		
		resultingHtml.append("\t<td>");
		resultingHtml.append("\t<select>");
		
		generatePersonelList(personelList,morningWorkShiftPlan.getPersonel().getName(),resultingHtml);
		resultingHtml.append("\t</select>");
		resultingHtml.append("</td>");
	}
	
	
	
	private void generatePersonelList(Personel[] personelList,String personelName,StringBuffer resultingHtml){
		for (Personel personel:personelList){
			if (personelName.equalsIgnoreCase(personel.getName())){
				resultingHtml.append("\t<option selected>" + personel.getName());
			}else{
				resultingHtml.append("\t<option>" + personel.getName());
			}
			resultingHtml.append("\t</option>");
		}
	}
	
	private WaitMonthPlanning getWaitMonthPlanning(HttpServletRequest request) {
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		if (year != null && year.length()>0 && month != null && month.length()>0){
			year = new Integer(Calendar.getInstance().get(Calendar.YEAR)).toString();
			month = new Integer(Calendar.getInstance().get(Calendar.MONTH)+1).toString();
		}

        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore = WaitWorkShiftDayPlanAppEngineDataStore.getInstance(ds);
        PlanningService planningService = PlanningPersistenceBasedService.getInstance(waitWorkShiftDayPlanDataStore);

		WaitMonthPlanning waitMonthPlanning = planningService.getWaitMonthPlanning(new Integer(year).intValue(), new Integer(month).intValue());
		return waitMonthPlanning;
	}

	private void performLoadWaitCalendar(StringBuffer resultingHtml,
			HttpServletRequest request) {
		DatastoreService dataStoreService = DatastoreServiceFactory.getDatastoreService();
		
		PersonelDataStore personelDataStore = PersonelAppEngineDataStore.getInstance(dataStoreService);
		
		PersonelService personelService = PersonelPersistenceBasedService.getInstance(personelDataStore);
		
		Personel[] personelList = (Personel[])personelService.getPersonelList().toArray(new Personel[personelService.getPersonelList().size()]);
		
		WaitMonthPlanning waitMonthPlanning = getWaitMonthPlanning(request);
		
		//generateWaitCalendarHeader(resultingHtml);
		
		for (Integer day:waitMonthPlanning.getWaitWorkShiftDayPlannings().keySet()){
			List<WaitWorkShiftDayPlan> waitWorkShiftPlan = waitMonthPlanning.getWaitWorkShiftDayPlanning(day);
			WaitWorkShiftDayPlan morningWorkShiftPlan = waitWorkShiftPlan.get(0);
			WaitWorkShiftDayPlan afterNoonShiftPlan = waitWorkShiftPlan.get(1);
			WaitWorkShiftDayPlan evevingShiftPlan = waitWorkShiftPlan.get(2);
			resultingHtml.append("<tr>");
			resultingHtml.append("\t<td colspan=\"2\">").append(day + "/" + waitMonthPlanning.getMonth() + "/"  +  waitMonthPlanning.getYear()).append("</td>");
			generateWorkShiftPlan(personelList,morningWorkShiftPlan,waitMonthPlanning,resultingHtml);
			generateWorkShiftPlan(personelList,afterNoonShiftPlan,waitMonthPlanning,resultingHtml);
			generateWorkShiftPlan(personelList,evevingShiftPlan,waitMonthPlanning,resultingHtml);
			resultingHtml.append("</tr>");
		}

	}
	
	private void performWaitCalendarTotals(StringBuffer resultingHtml,
			HttpServletRequest request) {
		WaitMonthPlanning waitMonthPlanning = getWaitMonthPlanning(request);
		Map<Personel,Integer> personelMonthTotals  = waitMonthPlanning.getPersonelMonthTotals();
		resultingHtml.append("<tr>");
		resultingHtml.append("<th colspan=\"4\"></th>");
		resultingHtml.append("<th>Adrukken</th>");
		resultingHtml.append("</tr>");
		
		for (Personel personel:personelMonthTotals.keySet()){
			Integer total = personelMonthTotals.get(personel);
			resultingHtml.append("<tr>");
			resultingHtml.append("<td colspan=\"2\">").append(personel.getName()).append("</td>");
			resultingHtml.append("<td colspan=\"3\">").append(total).append("</td>");
		}
		
		

	}
}